package com.example.demo.controller;

import com.example.demo.utils.WordExportUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * description :
 * author :song
 * create :2022-05-29 23:23:00
 */
@Slf4j
@RestController
@RequestMapping("/testmachine")
public class TestWordController {

    @ApiOperation("生成word文件")
    @GetMapping(value = "/downloadForSmall")
    public String downloadForSmall(HttpServletResponse response, @RequestParam("flag") String flag) throws IOException {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("name", "<w:p>\n" +
                "                        <w:pPr>\n" +
                "                            <w:rPr>\n" +
                "                                <w:rFonts w:hint=\"eastAsia\"/>\n" +
                "                                <w:lang w:val=\"en-US\" w:eastAsia=\"zh-CN\"/>\n" +
                "                            </w:rPr>\n" +
                "                        </w:pPr>\n" +
                "                        <w:r>\n" +
                "                            <w:rPr>\n" +
                "                                <w:rFonts w:hint=\"eastAsia\"/>\n" +
                "                                <w:lang w:val=\"en-US\" w:eastAsia=\"zh-CN\"/>\n" +
                "                            </w:rPr>\n" +
                "                            <w:t>1111</w:t>\n" +
                "                        </w:r>\n" +
                "                    </w:p>\n" +
                "                    <w:p>\n" +
                "                        <w:pPr>\n" +
                "                            <w:rPr>\n" +
                "                                <w:rFonts w:hint=\"default\"/>\n" +
                "                                <w:lang w:val=\"en-US\" w:eastAsia=\"zh-CN\"/>\n" +
                "                            </w:rPr>\n" +
                "                        </w:pPr>\n" +
                "                        <w:r>\n" +
                "                            <w:rPr>\n" +
                "                                <w:rFonts w:hint=\"eastAsia\"/>\n" +
                "                                <w:lang w:val=\"en-US\" w:eastAsia=\"zh-CN\"/>\n" +
                "                            </w:rPr>\n" +
                "                            <w:t>2222</w:t>\n" +
                "                        </w:r>\n" +
                "                        <w:bookmarkStart w:id=\"0\" w:name=\"_GoBack\"/>\n" +
                "                        <w:bookmarkEnd w:id=\"0\"/>\n" +
                "                    </w:p>\n" +
                "                    <w:sectPr>\n" +
                "                        <w:pgSz w:w=\"11906\" w:h=\"16838\"/>\n" +
                "                        <w:pgMar w:top=\"1440\" w:right=\"1800\" w:bottom=\"1440\" w:left=\"1800\" w:header=\"851\" w:footer=\"992\"\n" +
                "                                 w:gutter=\"0\"/>\n" +
                "                        <w:cols w:space=\"425\" w:num=\"1\"/>\n" +
                "                        <w:docGrid w:type=\"lines\" w:linePitch=\"312\" w:charSpace=\"0\"/>\n" +
                "                    </w:sectPr>");
        creatDoc(dataMap, flag);

        String codeByName = 3 + ".doc";
        String templateFile = this.getClass().getClassLoader().getResource("template/" + flag.toString() + ".ftl").getPath();
        String exportFile = templateFile.replace(".ftl", codeByName);

        //下面一行就是把对应路径下的文件转成输入流
        InputStream inputStream = new FileInputStream(exportFile);
        File file = new File(exportFile);


        response.reset();
        // 设置Content-Disposition响应头
        response.setHeader("Content-Disposition", "attachment;fileName=\"" + file.getName() + "\"");
        // 设置响应Content-Type
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    new String(codeByName.getBytes("UTF-8"), "ISO8859-1"));
            response.setHeader("FileName", new String(codeByName.getBytes("UTF-8"), "ISO8859-1"));
            response.setHeader("Access-Control-Expose-Headers", "FileName");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        InputStream in = inputStream;
        try {
            FileCopyUtils.copy(in, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return "下载文件出错";
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "success";
    }

    public void creatDoc(Map dataMap, String flag) throws IOException {
        //获取resources下的模板文件目录
        String templateFile = this.getClass().getClassLoader().getResource("template/" + flag.toString() + ".ftl").getPath();

        String exportFile = templateFile.replace(".ftl",  + 3 + ".doc");

        //String exportFile = "/usr/local/web/template/"+flag.toString()+userId.toString()+orgId.toString()+".doc";

        //String exportFile = "D:\\1.doc";
        //System.out.println("+++++++++++我生成的文件名和路径"+exportFile+"++++++++++++++++++++++");
        try {
            WordExportUtil.getInstance().createDocFile(templateFile, dataMap, exportFile, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
